package com.ssm.web.action;

import java.io.File;
import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.bean.DatabaseFile;
import com.ssm.util.Constant;
import com.ssm.util.DateUtil;
import com.ssm.util.FieldUtil;
import com.ssm.util.MessageUtil;
import com.ssm.util.Mysql;
import com.ssm.util.Page;
import com.ssm.util.common.BaseAction;
import com.ssm.web.service.DatabaseFileService;

@Controller
@RequestMapping("/sys")
public class DatabaseFileAction extends BaseAction {
	private String							actionname	= "数据库备份";
	private String							actionclass	= "DatabaseFile";
	@Autowired
	private DatabaseFileService	service;

	@RequestMapping(value = "/add2DatabaseFile.do")
	public String add2() {
		String file = getResourceFile() + File.separator + DateUtil.getCurrentTime(DateUtil.UTILTIME) + ".sql";
		File f = new File(file);
		try {
			DatabaseFile df = new DatabaseFile();
			df.setAddDate(DateUtil.getCurrentTime(DateUtil.FULL));
			df.setFilePath(f.getName());
			service.add(df);
			Mysql.backup(f.getAbsolutePath());
			MessageUtil.addMessage(request, "数据库备份成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				service.deleteFile(f.getName());
			} catch (Exception e1) {
			}
			MessageUtil.addMessage(request, "数据库备份失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/getDatabaseFile.do")
	public String get(int uid) {
		try {
			DatabaseFile temp = (DatabaseFile) service.get(uid);

			String file = getResourceFile() + File.separator + temp.getFilePath();
			File f = new File(file);
			if (!f.exists()) {
				MessageUtil.addMessage(getHttpServletRequest(), "数据库还原失败,备份文件未找到");
				return ERROR;
			}
			Mysql.load(f.getAbsolutePath());
			MessageUtil.addMessage(request, "数据库还原成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "数据库还原失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addDatabaseFile.do")
	public String add(DatabaseFile bean) {
		try {
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateDatabaseFile.do")
	public String update(DatabaseFile bean) {
		try {
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteDatabaseFile.do")
	public String delete(String ids) {
		try {
			service.deleteAll(ids);
			MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "操作失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryDatabaseFile.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, DatabaseFile.class, parmnames, parmvalues);

			Page page = service.selectPage(p, DatabaseFile.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listDatabaseFile";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
